﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Data.SqlClient;
using System.Text;

namespace Ccit.OA.PersonDoc
{
    public partial class persondoc_mydoc_download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strField = string.Empty;
            string strTitel = string.Empty;
            string strType = string.Empty;
            string strLength = string.Empty;
            //接收参数 表名，主键字段名称，主键值，图片字段名称
            if (Request.QueryString["Field"] != null)
            {
                strField = Request.QueryString["Field"].ToString().Trim();
            }
            else
            {
                Response.Write("<script language=javascript>alert('缺少参数!');</script>");
                Response.End();
            }

            string sSql = "SELECT DOC_A4.DOC_A4_30,Doc_a2.DOC_A2_40 as Title,DOC_A2_50 AS Type ,Doc_A2_60 AS Length FROM Doc_A4 left join DOC_A2 ON doc_a2.doc_a2_10 = doc_a4.doc_a4_10 WHERE DOC_A4_10 = " + strField;
            using (IDataReader iReader = DataAccessor.ExecuteReader(sSql))
            {
                SqlDataReader sqlReader;
                sqlReader = (SqlDataReader)iReader;
                if (sqlReader.Read())
                {
                    strTitel = sqlReader["Title"].ToString().Trim();
                    strType = sqlReader["Type"].ToString().Trim();
                    strLength = sqlReader["Length"].ToString().Trim();
                    CreateHeader(true, strTitel, strType);

                    Response.ContentType = strType;

                    Response.ContentEncoding = Encoding.GetEncoding("GB2312");
                    Response.Charset = "GB2312";
                    int iLen = Int32.Parse(strLength);
                    byte[] fileData = new byte[iLen];
                    long n = iReader.GetBytes(0, 0, fileData, 0, iLen);
                    Response.BinaryWrite(fileData);
                }
                else
                {
                    Response.Write("文件内容不存在或已丢失!");
                    Response.End();
                }
            } 
        }

        private void CreateHeader(bool isDown, string sFileName, string extName)
        {
            int iIndex = sFileName.LastIndexOf('.');
            sFileName = (iIndex == -1) ? sFileName + "." + extName : sFileName;
            string sEncodeFileName = sFileName;  

            int iMaxLen = 0;
            //Response.AppendHeader方法第二个参数的头部分

            string sParamTitle = "attachment;filename=";

            if (!isDown)
            {
                sParamTitle = "inline;filename=";
            }

            iMaxLen = 168 - sParamTitle.Length;

            //如果编码后的参数位数大于最大规定位数，则处理到合适的位数为止
            while (sEncodeFileName.Length > iMaxLen)
            {
                sFileName = sFileName.Remove(0, 1);
                //sEncodeFileName = HttpUtility.UrlEncode(sFileName);
                sEncodeFileName = sFileName;
            }

            Response.AppendHeader("content-disposition", sParamTitle + Server.UrlEncode(sEncodeFileName));// myReader.GetString(1));
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        }
    }
}
